package com.generalbackend.controller;

import com.generalbackend.common.annotion.ApiPermission;
import com.generalbackend.common.enums.MenuEnum;
import com.generalbackend.common.model.CommonResult;
import com.generalbackend.common.model.query.UpdatePasswordQuery;
import com.generalbackend.common.model.vo.UserVO;
import com.generalbackend.common.service.IPersonalInformationService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 个人信息
 * @author tim.xie
 * @date 2022/7/8 16:10
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/personal")
public class PersonalInformationController {

    @Autowired
    private IPersonalInformationService personalInformationService;

    @ApiOperation(httpMethod = "GET", value = "获取个人信息")
    @ApiPermission(name = "获取个人信息", menu = {MenuEnum.PersonalInformation})
    @RequestMapping(value = "/getPersonalInformation", method = RequestMethod.GET)
    public CommonResult getPersonalInformation(){
        return CommonResult.successCommonResult(personalInformationService.getPersonalInformation(),"查询成功");
    }

    @ApiOperation(httpMethod = "POST", value = "修改个人信息")
    @ApiPermission(name = "修改个人信息", menu = {MenuEnum.PersonalInformation})
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public CommonResult updateUser(@RequestBody UserVO userVO) {
        personalInformationService.updateUser(userVO);
        return CommonResult.successCommonResult("修改成功");
    }

    @ApiOperation(httpMethod = "POST", value = "上传用户头像")
    @ApiPermission(name = "上传用户头像", menu = {MenuEnum.PersonalInformation})
    @RequestMapping(value = "/uploadAvatar", method = RequestMethod.POST)
    public CommonResult uploadAvatar(@RequestParam("file") MultipartFile file) {
        String path = personalInformationService.uploadAvatar(file);
        if (StringUtils.isBlank(path)) {
            return CommonResult.errorCommonResult("上传用户头像失败");
        }
        String url = "/api/personal/downloadAvatar/" + path;
        return CommonResult.successCommonResult(url, "上传用户头像成功");
    }

    @ApiOperation(httpMethod = "GET", value = "下载用户头像")
    @ApiPermission(name = "下载用户头像")
    @RequestMapping(value = "/downloadAvatar/{childDir}/{randomFolder}/{filename}", method = RequestMethod.GET)
    public void downloadAvatar(HttpServletResponse response,
                                       @PathVariable("childDir") String childDir,
                                       @PathVariable("randomFolder") String randomFolder,
                                       @PathVariable("filename") String filename) {
        personalInformationService.downloadAvatar(response, childDir, randomFolder, filename);
    }

    @ApiOperation(httpMethod = "POST", value = "修改密码")
    @ApiPermission(name = "修改密码", menu = {MenuEnum.PersonalInformation})
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public CommonResult updatePassword(@RequestBody UpdatePasswordQuery updatePasswordQuery) {
        boolean result = personalInformationService.updatePassword(updatePasswordQuery);
        if (!result) {
            return CommonResult.errorCommonResult("修改密码失败");
        }
        return CommonResult.successCommonResult("修改密码成功");
    }

}
